#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import cgi
import datetime
import os
import lib
import logging
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext import db
from google.appengine.api import datastore_errors
from google.appengine.ext.webapp import template
from lib import llhandler
from model.models import *
from lib.workflowManager import WorkflowManager
class ModifyStepHandler(llhandler.PSHandler):
	def get(self,step_key):
	  self.auth_check()
	  self.internal_get(step_key)
	  
	def internal_get(self,step_key):
	  logging.debug("Modifying step")  
	  
	  step = PSStep.get(db.Key(encoded=step_key))
	  self.add("step",step)
	  
	  self.render('step_modify')
	  
	def post(self,step_key):
	  self.auth_check()
	  self.internal_post(step_key)
	  
	def internal_post(self,step_key):
	  step = PSStep.get(db.Key(encoded=step_key))
	  self.add("step",step)
	  try:
	    WorkflowManager().canProcessStep(step,self.logged_user,self.current_company)
	  except Exception,e:
	    logging.error('WFManager is not approving Step:'+str(e))
	    self.set_flash('Step could not be modified. ('+str(e)+')','error')
	    self.render()
	    return
	  try:
	    logging.debug("WFManager approves Step Processing")
	    step.status = self.request.get('status')
	    step.put()
	  except Exception,e:
	    self.set_flash('Step could not be modified. ('+str(e)+')','error')
	    self.render()
	    return
	  try:
	    step.modified = datetime.datetime.now()
	    step.modified_by = self.logged_user
	    step.put()
	    WorkflowManager().requestModified(step.request)
	    #Trying to modify request
	    #step.request.passed_steps.append(step.key())
	    #step.request.pending_steps.remove(step.key())
	  except Exception,e:
	    self.set_flash('Step parent request could not be updated. (' +str(e)+' )')
	    self.render()
	    return
	  self.set_flash('Step successfuly modified')
	  self.render()
	  
	  
  
class ShowStepHandler(llhandler.PSHandler):
	
	def get(self,step_id):
	  self.auth_check()
	  self.internal_get(step_id)
	  
	def internal_get(self,step_id):
	  step = PSStep.get(db.Key(encoded=step_id))
	  self.add("step",step)
	  
	  self.render('step_show')


def main():
  logging.getLogger().setLevel(logging.DEBUG)
  application = webapp.WSGIApplication([('/steps/modify/(\S+)',ModifyStepHandler),
					('/steps/(\S+)', ShowStepHandler),
					],
                                       debug=True)
  util.run_wsgi_app(application)


if __name__ == '__main__':
  main()
